package ${package.Controller};

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
import com.song.common.result.ResultEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestBody;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.Serializable;
/**
* <p>
    * ${table.comment!} 控制器
    * </p>
*
* @author ${author}
* @since ${date}
*/
@Api(tags = {"${table.comment!} 控制器"})
@RestController
@RequestMapping("/${table.name}")
public class ${table.controllerName} {

    @Autowired
    ${table.serviceName} ${table.entityPath}Service;

    /**
    * 分页查询数据
    *
    * @author ${author}
    * @Date ${date}
    */
    @ApiOperation(value = "按条件分页检索${table.comment!}",notes = "按条件分页检索${table.comment!}")
    @GetMapping(value = "/pageList")
    public ResultEntity<Page<${entity}>> pageList(
        @Validated ${entity} ${table.entityPath},
        @ApiParam(value = "页码", defaultValue = "1")
        @RequestParam(defaultValue = "1") int pageNum,
        @ApiParam(value = "分页大小", defaultValue = "10")
        @RequestParam(defaultValue = "10") int pageSize
    ) {
        Page<${entity}> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<${entity}> lambda = new QueryWrapper<${entity}>().lambda();
        <#list table.fields as filed>
        if(ObjectUtils.isNotEmpty(${table.entityPath}.get${'${filed.propertyName}'?cap_first}())){
            lambda.eq(${entity}::get${'${filed.propertyName}'?cap_first},${table.entityPath}.get${'${filed.propertyName}'?cap_first}());
        }
        </#list>
        return ResultEntity.success(${table.entityPath}Service.page(page,lambda));
    }

    /**
    * 查询数据List
    *
    * @author ${author}
    * @Date ${date}
    */
    @ApiOperation(value = "查询全部${table.comment!}",notes = "查询全部${table.comment!}")
    @GetMapping(value = "/list")
    public ResultEntity<List<${entity}>> list() {
        List<${entity}> ${table.entityPath}List = ${table.entityPath}Service.list();
        return ResultEntity.success(${table.entityPath}List);
    }

    /**
    * ${table.comment!}新增
    *
    * @author ${author}
    * @Date ${date}
    */
    @ApiOperation(value = "新增${table.comment!}",notes = "新增${table.comment!}")
    @PostMapping(value = "/add")
    public ResultEntity add(@RequestBody ${entity} ${table.entityPath}) {
        ${table.entityPath}Service.save(${table.entityPath});
        return ResultEntity.success();
    }

    /**
    * ${table.comment!}修改
    *
    * @author ${author}
    * @Date ${date}
    */
    @ApiOperation(value = "修改${table.comment!}",notes = "修改${table.comment!}")
    @PostMapping(value = "/update")
    public ResultEntity edit(@RequestBody ${entity} ${table.entityPath}) {
        ${table.entityPath}Service.updateById(${table.entityPath});
        return ResultEntity.success();
    }

    /**
    * 删除${table.comment!}
    *
    * @author ${author}
    * @Date ${date}
    */
    @ApiOperation(value = "按id删除${table.comment!}",notes = "按id删除${table.comment!}")
    @PostMapping(value = "/delete")
    public ResultEntity delete(@RequestParam Serializable id) {
        ${table.entityPath}Service.removeById(id);
        return ResultEntity.success();
    }


    /**
    * 根据id查询${table.comment!}
    *
    * @author ${author}
    * @Date ${date}
    */
    @ApiOperation(value = "根据id查询${table.comment!}",notes = "根据id查询修改${table.comment!}")
    @GetMapping(value = "/detail")
    public ResultEntity<${entity}> detail(@RequestParam Serializable id) {
        ${entity} ${table.entityPath} = ${table.entityPath}Service.getById(id);
        return ResultEntity.success(${table.entityPath});
    }
}